﻿var manager = null;

$(function () {
    $('#btnSave').click(function () {
        save();
    });

    $('#btnDelete').click(function () {
        del();
    });

    search();
});

//加载列表
function initTable(queryData, pageNumber) {
    manager = $("#maingrid").ligerGrid({
        height: '100%',
        url: '/Scheduling/SchedulingPersonnel/Search?ram=' + Math.random(),
        parms: queryData,
        usePager: false,
        rownumbers: false,
        heightDiff: 25,
        checkbox: true,
        columns: [
            { display: '工号', name: 'UserId', width: 150 },
            { display: '姓名', name: 'UserName', width: 150 }
        ],
        isChecked: function isChecked(data) {
            if (data.CheckState) {
                return true;
            }
            else {
                return false;
            }
        }
    });
    GridNoData($("#maingrid"));
}

//搜索
function search(pageNumber) {
    var queryData = {};
    initTable(queryData, pageNumber);
}

//保存
function save() {
    var list = [];
    var rows = manager.getCheckedRows();
    $(rows).each(function () {
        list.push({ PersonnelId: this.UserId });
    });
    $.ajax({
        url: "/Scheduling/SchedulingPersonnel/Save",
        type: "post",
        data: { personnelList: list },
        success: function (r) {
            var r = $.parseJSON(r);
            if (r.Result == 1) {
                $.ligerDialog.success("保存成功");
            }
            else if (r.Result == 0) {
                $.ligerDialog.error(r.Body);
            }
            else {
                $.ligerDialog.error("程序错误");
            }
        },
        error: function () {
            $.ligerDialog.error("程序错误");
        }
    });
}

//重置
function del() {
    $.ligerDialog.confirm('确定删除？', function (value) {
        if (value) {
            var url = "/Scheduling/SchedulingPersonnel/Delete";
            $.ajax({
                url: url,
                type: "POST",
                cache: false,
                success: function (data) {
                    var d = $.parseJSON(data);
                    if (d.Result == 1) {
                        $.ligerDialog.success("删除成功");
                        search();
                    }
                    else if (d.Result == 0) {
                        $.ligerDialog.error(d.Body);
                    }
                    else {
                        $.ligerDialog.error("程序错误");
                    }
                },
                error: function () {
                    $.ligerDialog.error("程序错误");
                }
            });
        }
    });
}